// pages/goodsList/index.js

const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        products: null,
        introduction: [],
        active: true,
        hidden: true,
        num: 1,
        Favorite:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const products = JSON.parse(options.products) //获取跳转时传递的商品数据
        const favorite = wx.getStorageSync('favorite')||[]
        let index = favorite.findIndex(v=>{return v.fid===products.fid})
         let Favorite = index>-1?true:false
        this.setData({
            products,
            Favorite
        })
    },
    Favorite(){
      this.setData({Favorite:app.Favorite(this.data.products)})
    },
    handlepreviewImage(e) {
        // 点击图片时展示对应图片
        const urls = this.data.products.img
        const index = e.currentTarget.dataset.idx
        wx.previewImage({
            urls: urls,
            current: urls[index]
        })
    },
    Addshopping() {
        // 添加商品时方法
        var hidden = this.data.hidden
        if (hidden == true) { //判断是否已打开菜单
            this.setData({
                hidden: !hidden
            })
            return
        }
        let active = this.data.active
        let products = this.data.products
        let num = this.data.num
        app.cal(active, products, this, num, "hidden") //封装好的添加到购物车方法
        this.setData({
            num:1,
          })
    },
    BuyNow() {
        var hidden = this.data.hidden
        if(app.globalData.login===false){
          wx.showToast({
            title: '请先登录',
            icon:"none"
          })
          setTimeout(function(){
              wx.switchTab({
                  url: '/pages/personal/index'
                })
          },1000)
          
          return false
      }
        if (hidden == true) { //判断是否已打开菜单
            this.setData({
                hidden: !hidden
            })
            return
        }
        this.setData({
            ["products.num"]:this.data.num
        })
        var products =JSON.stringify(this.data.products);
        console.log(products);
        
        wx.navigateTo({
          url: '/pages/pay/index?products='+products,
        })
    },
    hid() {
        this.setData({ //切换显示菜单
            hidden: !this.data.hidden
        })
    },
    Act() {
        this.setData({ //切换选择规格
            active: !this.data.active
        })
    },
    MathNum(e) { //计算购买商品数量
        let name = e.currentTarget.dataset.name
        let num = this.data.num
        let value = e.detail.value - 0
        app.MathNum(name, num, this, value)
    },
})